package com.teamin.service.impl;

import com.teamin.config.ErrorCode;
import com.teamin.entity.WxGroupEntity;
import com.teamin.mapper.WxGroupMapper;
import com.teamin.service.WxGroupService;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class WxGroupServiceImpl implements WxGroupService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private WxGroupMapper wxGroupMapper;

	@Override
	public int saveGroup(WxGroupEntity wxGroupEntity) throws GlobalErrorInfoException {
		// 已经保存过
		if (wxGroupMapper.groupSaved(wxGroupEntity.getGroupId())) {
			return 1;
		}
		// 没有保存过
		else {
			try {
				int i = wxGroupMapper.saveGroup(wxGroupEntity);
				if (i == 0) {
					throw new GlobalErrorInfoException(
							new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.ADD_WXGROUP_FAIL));
				}
				return i;
			} catch (Exception e1) {
				throw new GlobalErrorInfoException(
						new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.ADD_WXGROUP_FAIL));
			}
		}
	}

	@Override
	public WxGroupEntity selectWxGroupEntity(String groupId) throws GlobalErrorInfoException {
		try {
			WxGroupEntity wxGroupEntity = wxGroupMapper.selectWxGroupEntity(groupId);
			if (wxGroupEntity == null) {
				throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FIND_WXGROUP_NULL));
			}
			return wxGroupEntity;
		} catch (Exception e) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.FIND_WXGROUP_NULL));
		}
	}

	@Override
	public int updateWxGroupEntity(WxGroupEntity wxGroupEntity) throws GlobalErrorInfoException {
		try {
			int i = wxGroupMapper.updateWxGroupEntity(wxGroupEntity);
			if (i == 0) {
				throw new GlobalErrorInfoException(
						new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.UPDATE_WXGROUP_FAIL));
			}
			return i;
		} catch (Exception e) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.ADD_WXGROUP_FAIL));
		}
	}

	@Override
	public int addWxGroupEntityToCircle(WxGroupEntity wxGroupEntity, Long circleId,Long userId) throws GlobalErrorInfoException {

		String groupId = wxGroupEntity.getGroupId();

		// 保存群
		saveGroup(wxGroupEntity);

		// 已经关联过
		if (wxGroupMapper.groupLined(groupId, circleId)) {
			return 1;
		}
		// 没有关联过
		else {
			try {
				int i = wxGroupMapper.addWxGroupEntityToCircle(groupId, circleId,userId);
				if (i == 0) {
					throw new GlobalErrorInfoException(
							new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.LINK_WXGROUP_FAIL));
				}
				return i;
			} catch (Exception e) {
				throw new GlobalErrorInfoException(
						new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.LINK_WXGROUP_FAIL));
			}
		}

	}

	@Override
	public int removeWxGroupEntityFromCircle(String groupId, Long circleId) throws GlobalErrorInfoException {
		try {
			int i = wxGroupMapper.removeWxGroupEntityFromCircle(groupId, circleId);
			if (i == 0) {
				throw new GlobalErrorInfoException(
						new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.UNLINK_WXGROUP_FAIL));
			}
			return i;
		} catch (Exception e) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.UNLINK_WXGROUP_FAIL));
		}
	}

	@Override
	public List<WxGroupEntity> selectwxGroupEntitys(Long circleId) {
		return wxGroupMapper.selectwxGroupEntitys(circleId);
	}

	/**
	 * 获取圈子中所关联的微信群
	 * @param  circleId 当前的圈子
	 * @return 微信群ID列表
	 */
	@Override
	public List<WxGroupEntity> getGroupsByCircle(Long circleId) {
		return wxGroupMapper.getGroupsByCircle(circleId);
	}

	/**
	 * 查询圈子有对应的微信群ID
	 * @param circleId
	 * @return
	 */
	@Override
	public Boolean existGroupCircle(String groupId, Long circleId){
		return wxGroupMapper.existGroupCircle(groupId,circleId) ;
	}


	/**
	 * 保存圈子中用户的关联关系
	 * @param userId
	 * @param groupId
	 */
	@Override
	public void saveUserGroup(Long userId,String groupId) {
		if(!wxGroupMapper.selectGroupUser(userId,groupId)) {
			wxGroupMapper.insertGroupUser(userId,groupId) ;
		}
	}

}
